import * as actions from '../actions/index';
import utils from 'utils';

const navKey = utils.getUrlSearchParam('navKey') || 'mine';

// 对页面prop 数据进行管理
export const initialState = {
  groupList: [],
  search: '',
  isLoading: true,
  total: 0,
  currentPage: 1,
  navKey,
  order_by: 'updated_at',
  archived_repository: false,
  subGroupMap: {},
  group_creator_role: [],
  repoCreatorIdentity: [],
  create_group: {},
  create_repo: {},
  is_max_level: false,
  pageSize: 10,
  group_max_level: null,
  createRepositorySuccess: false,
};
const defaultAction = {
  type: 'doNothing',
};

export default function index(state = initialState, action = defaultAction) {
  const newSubGroup = state.subGroupMap;
  switch (action.type) {
    case actions.UPDATE_DATA:
      return { ...state, ...action.data };
    case actions.RESET_DATA: {
      const newState = JSON.parse(JSON.stringify(initialState));
      newState.createRepositorySuccess = state.createRepositorySuccess;
      newState.pageSize = state.pageSize;
      return newState;
    }
    case actions.RECEIVE_GROUP_SUBLIST:
      return { ...state, subGroupMap: { ...state.subGroupMap, ...action.data } };
    case actions.UPDATE_GROUP_SUBLIST:
      delete newSubGroup[action.data];
      return { ...state, subGroupMap: { ...newSubGroup } };
    case actions.RECEIVE_CREATE_ACCESS:
      return {
        ...state,
        create_group: action.data.create_group,
        create_repo: action.data.create_repo,
        is_max_level: action.data.is_max_level,
        group_max_level: action.data.group_max_level,
      };

    default:
      return state;
  }
}
